package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
)

/*
   Author: mahaiyuan
   Date: 2021/2/24 4:20 下午
   Description:
*/

func main() {
	db, err := gorm.Open("mysql", "root:12345678@tcp(localhost:3306)/gogogo?charset=utf8mb4&parseTime=True&loc=Local")
	db.LogMode(true)	//开启日志 打印执行的sql
	if err != nil {
		panic("fail to connect db")
	}
	db.SingularTable(true)
	defer db.Close()

	fmt.Println(db)

	var product Product
	rs := db.HasTable(product)
	fmt.Println("table exists ?", rs)
	if !rs {
		db.AutoMigrate(&Product{})
	}
	//create := db.Create(&Product{101, "golang入门"})
	//fmt.Printf("resutl is %v\n", create)
	db.First(&product, 101)
	fmt.Printf("product is %v \n", product)
}

type Product struct {
	Id int64
	Name string
}